package com.hanlyjiang.library.components;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hanlyjiang.library.R;

/**
 * 带有波纹和标题的 Toolbar，用于统一各个页面的标题样式
 * <br/><br/>
 * 有 2 个xml 属性可用：
 * <li>1. title  -  用于设置标题 </li>
 * <li>2. enableBackExit - 默认为true； 设置为 true 时显示左边的回退按钮，并且点击该按钮会回退; 设置为false时 不显示该按钮 </li>
 * <li>3. showWave - 默认为true； 设置为 true 时显示下面的波纹，否则不显示 </li>
 * 另可以通过{@link #setOnBackClickListener(OnBackingListener)} 方法拦截返回按钮的点击事件
 *
 * @author hanlyjiang on 2017/6/2-下午5:28.
 * @version 1.0
 */

public class CommonWaveToolbar extends BaseCommonLinearLayout {

    private Toolbar toolbar;
    private TextView titleTv;
    private View waveView;
    private OnBackingListener onBackClickListener;

    public CommonWaveToolbar(Context context) {
        super(context);
    }

    public CommonWaveToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CommonWaveToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CommonWaveToolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        setOrientation(VERTICAL);
        // <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
        View childRoot = LayoutInflater.from(context)
                .inflate(R.layout.toolbar_with_wave_layout, this, true);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        titleTv = (TextView) findViewById(R.id.toolbar_center_title);
        waveView = findViewById(R.id.iv_wave);

        TypedArray typedArray = retrTypedArray(context, attrs, R.styleable.CommonWaveToolbar);
        String titleText = typedArray.getString(R.styleable.CommonWaveToolbar_title);
        setTitle(TextUtils.isEmpty(titleText) ? "" : titleText);

        boolean enableExitBack = typedArray.getBoolean(R.styleable.CommonWaveToolbar_enableBackExit, true);
        if (enableExitBack) {
            toolbar.setNavigationIcon(R.drawable.ic_back);
            toolbar.setNavigationOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean goBackExit = true;
                    if (onBackClickListener != null) {
                        goBackExit = !onBackClickListener.onInteruptGoBackEvent();
                    }
                    if (goBackExit) {
                        Activity activity = getActivity(v.getContext());
                        if (activity != null) {
                            activity.onBackPressed();
                        }
                    }
                }
            });
        }

        boolean showWave = typedArray.getBoolean(R.styleable.CommonWaveToolbar_showWave, true);
        showOrHideWave(showWave);

        typedArray.recycle();
    }


    private Activity getActivity(Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return getActivity(((ContextWrapper) context).getBaseContext());
        }
        return null;
    }

    /**
     * 获取Toolbar
     *
     * @return
     */
    public Toolbar getToolbar() {
        return toolbar;
    }

    /**
     * 获取标题TextView
     *
     * @return
     */
    public TextView getTitleTv() {
        return titleTv;
    }

    public void setTitle(String title) {
        titleTv.setText(title);
    }


    /**
     * 设置回退按钮的事件拦截监听
     *
     * @param onBackClickListener 如果需要拦截，请在 onInteruptGoBackEvent() 方法中返回 true
     */
    public void setOnBackClickListener(OnBackingListener onBackClickListener) {
        this.onBackClickListener = onBackClickListener;
    }

    public void showOrHideWave(boolean toShow) {
        waveView.setVisibility(toShow ? View.VISIBLE : View.GONE);
    }

    public interface OnBackingListener {
        /**
         * 如果拦截事件，则返回true，否者返回false
         */
        boolean onInteruptGoBackEvent();
    }
}
